package zaijiansunwukong;


import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.junit.Test;
import zaijiansunwukong.util.FreemarkerUtils;
import zaijiansunwukong.util.PdfUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/12.
 */
public class HtmlToPdfFreemarker {
	Logger logger = Logger.getLogger(HtmlToPdfFreemarker.class);
	@Test
	public void html2Pdf() {
		try {
			Map map = new HashMap();
			// String html = FreemarkerUtils.processTemplate("example.ftl", map);
			String html = FreemarkerUtils.processTemplate("simple.ftl", map);
			byte[] pdfBytes = PdfUtils.htmlToPdf(html);
			String allPath = "simple.pdf";
			FileOutputStream outputStream = null;
			try {
				outputStream = new FileOutputStream(allPath);
				IOUtils.write(pdfBytes, outputStream);

			} catch (IOException e) {
				logger.error("", e);

			} catch (Exception e1) {
				logger.error("", e1);

			} finally {
				try {
					if (outputStream != null) {
						outputStream.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
